import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import { Control } from 'react-keeper'
import update from 'react-addons-update';
import DB from './../../../app/db';

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    //滚动组件
    infs: null,

    //查询到的日志
    rzls: [],

    //选中的日志
    currentObj: {},

    //搜索关键字
    keyword: '',

    //当前页数
    currentPage: 1,
    pageSize: 10,
    isUpload: false,
    lastPage: false,
    //本弹窗是否显示
    fullpage_show: false,


    items: []
  },

  loadMode: function (params) {
    let t = this;
    /*this.data.jlmode = update(this.data.jlmode,  {$set: params});*/
    this.data.readOnly = false;
    this.data.operType = 'update';
    t.updateComponent();
  },

  onsetCurrentObj: function (params) {
    console.info(params);
    this.data.currentObj = update(this.data.currentObj, {$set: params});
    console.info('select');
    console.info(this.data.currentObj);
    this.updateComponent();
  },

  onGoInfo: function (obj) {
    console.log('返回2');
    console.info(obj);
    if(obj.router==='')
    {
      obj.router = '/home'
    }
    Control.go(obj.router,obj);
  },


  //初始化方法
  onInit: function () {
    this.data.fullpage_show = update(this.data.fullpage_show, {$set: false});
    this.data.currentPage = update(this.data.currentPage, {$set: 1});
    this.data.currentObj = update(this.data.currentObj, {$set: {}});
    this.data.rzls = update(this.data.rzls, {$set: []});
    this.data.items = update(this.data.items, {$set: []});

    this.onFetchOne({
      page: 1,
      size: 50,
      keyword:'',
      userid:'',
      isUpload:'',
    });
    this.updateComponent();
  },

  onInitFetch: function (params) {
    let t = this;
    console.info('1日志1');
    console.info(params);
    this.onJlFetch({
      page: 1,
      size: 50,
      keyword:'',
      userid:'13972608122',
      isUpload:'',
    });
    t.updateComponent();
  },

  onFetchOne: function (params) {

    let t = this;
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.isUpload = t.data.isUpload;

    DB.WechatAPI.findRiZhiList(params).then(
      function (content) {
        if (content.length <= 0) {
          console.info('未查找到数据');
        }
        else
        {
          t.data.items = update(t.data.items, {$set: content.content});
          // t.data.currentPage = update(t.data.currentPage, {$set: content.size});

        }
        //如果请求后数据数量小于pageSize则设置为最后一页
        if (t.data.items.length < 10) {
          t.data.lastPage = update(t.data.lastPage, {$set: true});
        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
  },

  onJlSearch: function (params) {
    let t = this;
    console.info('1日志1');
    console.info(params);
    this.onJlFetch({
      page: 1,
      size: 50,
      keyword:'',
      userid:'13972608122',
      isUpload:'',
    });
    t.updateComponent();
  },

  onJlFetch: function (params) {

    let t = this;
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.isUpload = t.data.isUpload;

    DB.WechatAPI.findRiZhiList(params).then(
      function (content) {
        if (content.length <= 0) {
          console.info('未查找到数据');
        }
        else
        {
          t.data.rzls = update(t.data.rzls, {$set: content.content});
          t.data.items = update(t.data.items, {$push: t.data.rzls});
         // t.data.currentPage = update(t.data.currentPage, {$set: content.size});
          console.info('循环查询');
          console.info(t.data.rzls);
        }
        //如果请求后数据数量小于pageSize则设置为最后一页
        if (t.data.rzls.length < 10) {
          t.data.lastPage = update(t.data.lastPage, {$set: true});
        }
        t.updateComponent();
      },
      function (error) {
        t.data.lastPage = update(t.data.lastPage, {$set: true});
        console.log(error);
      });
  },

  onAddPage(){
    console.info('加载查询');
    this.data.rzls = update(this.data.rzls, {$set: []});
    this.data.currentPage = update(this.data.currentPage,  {$set: this.data.currentPage+1});
    console.info('页数'+this.data.currentPage);
    this.onJlFetch({
      page: 1,
      size: 50,
      keyword:'',
      userid:'13972608122',
      isUpload:'',
    });
    console.info('加载查询完毕');
  },


  initMode: function () {
    let t = this;
    t.data.operType == 'add'
    t.updateComponent();
  },

  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }


});
